<template>
  <div class="header clearFixed">
    <div class="zx-introduce">
      <img :src="require('../assets/imgs/logo.png')" alt="logo"/>
    </div>
    <div class="menu-btn" @click="showMenus"></div>
    <div class="zx-menu-wrapper">
      <ul class="zx-menu clearFixed">
        <li v-for="(item, i) in menus"
            :key="item.name"
            :class="{active: i === select}"
            @click="liEvent(i, item.link, item.child)">
          {{item.name}}
          <div class="zx-sub-menu-wrapper" v-if="item.child.length > 0">
            <ul class="zx-sub-menu">
              <li v-for="(item1, j) in item.child"
                  :key="item1.name"
                  :class="{active: `${i}${j}` === subSelect}"
                  @click="subLiEvent(i, j, item1.link)">{{item1.name}}
              </li>
            </ul>
          </div>
        </li>
      </ul>
      <ul class="zx-menu-m clearFixed" :class="{active: showMenu}">
        <li v-for="(item, i) in menus"
            :key="item.name"
            :class="{active: i === select}"
            @click="liEvent_m(i, item.link, item.child)">
          {{item.name}}
          <div class="zx-sub-menu-wrapper-m" v-if="item.child.length > 0">
            <ul class="zx-sub-menu-m">
              <li v-for="(item1, j) in item.child"
                  :key="item1.name"
                  :class="{active: `${i}${j}` === subSelect}"
                  @click.stop="subLiEvent(i, j, item1.link)">{{item1.name}}
              </li>
            </ul>
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
  import {EventHub} from '../common/EventHub';

  export default {
    name: 'Navigator',
    data() {
      return {
        select: 0,
        subSelect: '',
        menus: [
          {
            name: '首页',
            child: [],
            link: '/'
          },
          {
            name: '产品解决方案',
            child: [
              {name: '智能服务解决方案', link: '/Service'},
              {name: '智能运维解决方案', link: '/SmartOperation'},
              {name: '企业智能化解决方案', link: '/Enterprise'},
              {name: '智能汽车解决方案', link: '/Vehicle'}
            ],
            link: ''
          },
          {
            name: '智询社区',
            child: [],
            link: ''
          },
          {
            name: '关于我们',
            child: [],
            link: ''
          }
        ],
        currentPath: '',
        showMenu: false
      };
    },
    mounted() {
      this.setCurrentMenu(this.$route.path);
      EventHub.$on('path-change', (path) => {
        this.setCurrentMenu(path);
      });
    },
    methods: {
      setCurrentMenu(path) {
        this.currentPath = path;
        const p = this.menus.map((item, i) => {
          if (item.link) {
            return item.link;
          } else if (item.link === '' && item.child.length > 0) {
            return item.child.map(item1 => item1.link);
          }
        }).filter(item => item !== undefined);

        for (let i = 0; i < p.length; i++) {
          const _path = p[i];
          const j = _path.indexOf(path);
          if (j !== -1) {
            this.select = i;
            if (Object.prototype.toString.call(_path) === '[object Array]') {
              this.subSelect = `${i}${j}`;
            }
            break;
          }
        }
      },
      liEvent(i, link, child) {
        if (link && this.select !== i) {
          this.select = i;
          this.subSelect = '';
          this.$router.push(link);
        }
      },
      liEvent_m(i, link, child) {
        if (link || child.length > 0) {
          this.subSelect = '';
          this.select = i;
          if (link && link !== this.currentPath) {
            this.$router.push(link);
            this.showMenu = false;
          }
        }
      },
      subLiEvent(i, j, link) {
        if (link !== this.currentPath) {
          this.select = i;
          this.subSelect = `${i}${j}`;
          this.$router.push(link);
          this.showMenu = false;
        }
      },
      showMenus() {
        if (!this.showMenu) {
          this.setCurrentMenu(this.currentPath);
        }
        this.showMenu = !this.showMenu;
      }
    }
  };
</script>

<style lang="less">
  .header {
    width: 100%;
    height: 65px;
    padding: 0 50px;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 2;
  }

  .menu-btn {
    display: none;
  }

  .zx-introduce {
    float: left;
    display: inline-flex;
    height: 100%;
    justify-content: center;
    align-items: center;

    img {
      height: 50px;
    }
  }

  .zx-menu-wrapper {
    height: 100%;
    float: right;
  }

  .zx-menu-m {
    display: none;
  }

  .zx-menu {
    height: 100%;
    display: flex;
    align-items: center;
  }

  .zx-menu > li {
    float: left;
    margin: 0 10px;
    cursor: pointer;
    position: relative;
    width: 120px;
  }

  .zx-menu > li.active {
    color: #FF6C17;
  }

  .zx-menu > li.active::after {
    content: '';
    position: absolute;
    top: calc(100% + 10px);
    left: 0;
    width: 100%;
    height: 2px;
    background-color: #FF6C17;
  }

  .zx-menu > li:hover > .zx-sub-menu-wrapper {
    display: block;
  }

  .zx-sub-menu-wrapper {
    display: none;
    padding-top: 20px;
    position: absolute;
    top: 100%;
    left: 0;
    width: max-content;
  }

  .zx-sub-menu {
    border: 1px solid #dddddd;
    padding: 0 10px;
    background-color: rgba(48, 28, 127, 0.8);
  }

  .zx-sub-menu > li {
    font-size: 14px;
    padding: 10px 5px;
  }

  .zx-sub-menu > li.active,
  .zx-sub-menu > li:hover {
    background-color: #301C7F;
    color: #FF6C17;
  }

  .zx-sub-menu > li:not(:last-child) {
    border-bottom: 1px solid #ddd;
  }

  @media screen and (max-width: 768px) {
    .menu-btn {
      display: block;
      width: 30px;
      height: 30px;
      background-color: #301C7F;
      position: absolute;
      top: 10px;
      right: 20px;
      border: 1px solid #ffffff;
      border-radius: 5px;
      background-image: url("../assets/imgs/menu.png");
      background-position: center;
      background-repeat: no-repeat;
      background-size: contain;
    }

    .header {
      width: 100%;
      height: 55px;
      padding: 0 20px;
      position: absolute;
      top: 0;
      left: 0;
      z-index: 2;
    }

    .zx-introduce {
      float: left;
      display: inline-flex;
      height: 100%;
      justify-content: center;
      align-items: center;

      img {
        height: 40px;
      }
    }

    .zx-menu-wrapper {
      height: unset;
      width: 100%;
      position: absolute;
      left: 0;
      top: 65px;
    }

    .zx-menu {
      display: none;
    }

    .zx-menu-m {
      background-color: #4960FC;
      box-shadow: 0 1px 5px 0px #ddd;
    }

    .zx-menu-m.active {
      display: block;
    }

    .zx-menu-m > li {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      cursor: pointer;
      position: relative;
      width: 100%;
      padding: 20px 0;

      &:not(:last-child) {
        border-bottom: 1px solid #ddd;
      }
    }

    .zx-menu-m > li.active {
      color: #FF6C17;
    }

    .zx-menu-m > li.active > .zx-sub-menu-wrapper-m {
      height: unset;
      padding-top: 20px;
    }

    .zx-sub-menu-wrapper-m {
      height: 0;
      overflow: hidden;
      width: 100%;
      transition: all 0.3s;
    }

    .zx-sub-menu-m {
      border: 1px solid #dddddd;
      padding: 0 10px;
      background-color: rgba(48, 28, 127, 0.8);
    }

    .zx-sub-menu-m > li {
      font-size: 14px;
      padding: 15px 5px;
    }

    .zx-sub-menu-m > li.active,
    .zx-sub-menu-m > li:hover {
      background-color: #301C7F;
      color: #FF6C17;
    }

    .zx-sub-menu-m > li:not(:last-child) {
      border-bottom: 1px solid #ddd;
    }
  }
</style>
